package com.niodata.dp.core.service;

import com.niodata.dp.core.resource.ResourceType;
import com.niodata.dp.log.LogUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

public class SystemServices {

  private static final Logger logger = Logger.getLogger(SystemServices.class);
  private static final List<SystemService> systemServiceList = new ArrayList<>();
  private static final Map<ResourceType, SystemService> resource2ServiceMap = new HashMap<>();
  private static final Map<String, SystemService> resourceName2ServiceMap = new HashMap<>();
  private static final Class[] serviceClazzs = new Class[]{
      UserMgrService.class, GroupMgrService.class, DwMgrService.class,
      PluginMgrService.class, JobMgrService.class, FileMgrService.class, DiService.class,
      ObjectMgrService.class, NoteBookService.class
  };

  static {
    for (Class clazz : serviceClazzs) {
      try {
        SystemService systemService = (SystemService) clazz.newInstance();
        systemServiceList.add(systemService);
        resource2ServiceMap.put(systemService.getResourceType(), systemService);
        resourceName2ServiceMap.put(systemService.getName(), systemService);
      } catch (InstantiationException e) {
        LogUtil.logStackTrace(logger, e);
      } catch (IllegalAccessException e) {
        LogUtil.logStackTrace(logger, e);
      }
    }

  }

  private SystemServices() {
  }

  public static List<SystemService> getServcieList() {

    return systemServiceList;
  }

  public static SystemService getSystemService(String servicename) {
    return resourceName2ServiceMap.get(servicename);
  }

  public static SystemService getSystemService(ResourceType resourceType) {
    return resource2ServiceMap.get(resourceType);
  }

  public static class UserMgrService implements SystemService {

    UserMgrService() {

    }

    @Override
    public ResourceType getResourceType() {
      return ResourceType.User;
    }

    @Override
    public String getName() {
      return ResourceType.User.getTypeName();
    }

    @Override
    public String getDescription() {
      return "用户管理服务";
    }
  }

  public static class GroupMgrService implements SystemService {

    GroupMgrService() {
    }

    @Override
    public ResourceType getResourceType() {
      return ResourceType.Group;
    }

    @Override
    public String getName() {
      return ResourceType.Group.getTypeName();
    }

    @Override
    public String getDescription() {
      return "组管理服务";
    }
  }

  public static class JobMgrService implements SystemService {

    JobMgrService() {
    }

    @Override
    public ResourceType getResourceType() {
      return ResourceType.Job;
    }

    @Override
    public String getName() {
      return ResourceType.Job.getTypeName();
    }

    @Override
    public String getDescription() {
      return "任务管理服务";
    }
  }

  public static class PluginMgrService implements SystemService {

    PluginMgrService() {
    }

    @Override
    public ResourceType getResourceType() {
      return ResourceType.Plugin;
    }

    @Override
    public String getName() {
      return ResourceType.Plugin.getTypeName();
    }

    @Override
    public String getDescription() {
      return "插件管理服务";
    }
  }


  public static class DwMgrService implements SystemService {

    DwMgrService() {
    }

    @Override
    public ResourceType getResourceType() {
      return ResourceType.Db;
    }

    @Override
    public String getName() {
      return ResourceType.Db.getTypeName();
    }

    @Override
    public String getDescription() {
      return "数据仓库管理服务";
    }
  }


  public static class ObjectMgrService implements SystemService {

    ObjectMgrService() {
    }


    @Override
    public ResourceType getResourceType() {
      return ResourceType.Object;
    }


    @Override
    public String getName() {
      return ResourceType.Object.getTypeName();
    }

    @Override
    public String getDescription() {
      return "对象存储服务";
    }
  }


  public static class FileMgrService implements SystemService {

    FileMgrService() {

    }

    @Override
    public ResourceType getResourceType() {
      return ResourceType.File;
    }

    @Override
    public String getName() {
      return ResourceType.File.getTypeName();
    }

    @Override
    public String getDescription() {
      return "文件管理服务";
    }
  }

  public static class NoteBookService implements SystemService {

    NoteBookService() {

    }

    @Override
    public ResourceType getResourceType() {
      return ResourceType.NoteBook;
    }

    @Override
    public String getName() {
      return "NoteBook";
    }

    @Override
    public String getDescription() {
      return "NoteBook服务";
    }
  }

  public static class DiService implements SystemService {

    DiService() {
    }

    @Override
    public ResourceType getResourceType() {
      return ResourceType.DataEntityInterface;
    }

    @Override
    public String getName() {
      return "DiRes";
    }

    @Override
    public String getDescription() {
      return "数据实体查询服务";
    }
  }
}
